<?php

class SmsHelper extends Helper {

    private $_verifyfield;
    private $verify_rule;

    public function __construct() {
        $this->_verifyfield = array('sms_mobile', 'sms_code', 'sms_dateline');
        $this->verify_rule = array(
            'sms_mobile' => array('mobile', '手机号不能为空')
        );
    }

    public function verifySave($data) {
        $validator = Validator::checkRule($data, $this->verify_rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $model = $this->LoadModel('Public');
        $id = $model->save('sms_verify', $data);
        if ($id) {
            return array('success' => true, 'msg' => $id);
        } else {
            return array('success' => false, 'msg' => '保存失败');
        }
    }
    
    public function verifyRow($where){
        if(is_numeric($where)){
            $where = array(
                '`sms_mobile` = ?' => $where
            );
        }
        $model = $this->LoadModel('Public');
        return $model->getRow('sms_verify', $this->_verifyfield, $where, null, array('sms_dateline DESC'));
    }
    
    public function smsVerify($mobile, $code){
        $check = $this->verifyRow(array(
            '`sms_mobile` = ?' => $mobile,
            '`sms_dateline` >= ?' => NOW_TIME - 300 
        ));
        if(empty($check)){
            return false;
        }elseif($check['sms_code'] == $code){
            return true;
        }else{
            return false;
        }
    }
    
}